﻿using System.Collections.ObjectModel;
using DuSQL.API;
using System.Collections.Generic;
using System.Linq;
using System.Data;
namespace DuSQL.ViewModel
{
    public class AllConnectsViewModel
    {
         readonly ReadOnlyCollection<DbConnectViewModel> _dbconnect;

         public AllConnectsViewModel()
        {
            _dbconnect = new ReadOnlyCollection<DbConnectViewModel>(
                (from region in DuSQL.API.Comm.MyConnections select new DbConnectViewModel(region))
                .ToList());
        }

        public ReadOnlyCollection<DbConnectViewModel> Connects
        {
            get { return _dbconnect; }
        }
    }
    public class DbConnectViewModel : TreeViewItemViewModel
    {
        readonly DbConnect connect;
        public string ConnectionStr{get{return connect.ConnectStr;}}
        public DbConnectViewModel(DbConnect conn)
            : base(null, true)
        {
            connect = conn;           
        }

        public string ConnectName
        {
            get { return connect.ConnectName; }
        }        


        protected override void LoadChildren()
        {
            base.Children.Add(new DbElementViewModel("表", this, NodeType.DbFolder_Table));
            base.Children.Add(new DbElementViewModel("视图", this, NodeType.DbFolder_View));
            base.Children.Add(new DbElementViewModel("存储过程", this, NodeType.DbFolder_Procedure));
        }
        public NodeType NodeType { get { return connect.DbType; } }
    }

    public class DbElementViewModel : TreeViewItemViewModel
    {
        //显示的目录“表，视图，存储过程”;        
        public DbElementViewModel(string name, DbConnectViewModel parent, NodeType nodetype)
            : base(parent, true)
        {
            DbElementName = name;
            NodeType = nodetype;
            ConnectStr=parent.ConnectionStr;
            DataBaseType = parent.NodeType;
        }
        public string DbElementName { get; set; }
        public NodeType NodeType { get; set; }
        public string ConnectStr{get;set;}
        public NodeType DataBaseType { get; set; }

        protected override void LoadChildren()
        {
            switch(this.NodeType)
            {
                case NodeType.DbFolder_Table:
                    {
                        
                        DataTable dt=BLLDbHelper.Instance.GetShemaTable(ConnectStr,DataBaseType);
                        foreach (DataRow item in dt.Rows)
                        {
                            base.Children.Add(new TableInfoViewModel(item["table_name"].ToString(),this));
                        }   
                        break;
                    }
                case NodeType.DbFolder_View:
                    {
                        DataTable dt =BLLDbHelper.Instance.GetShemaView(ConnectStr,DataBaseType);
                        foreach (DataRow item in dt.Rows)
                        {
                            base.Children.Add(new TableInfoViewModel(item["table_name"].ToString(), this));
                        }
                        break;
                    }
                case NodeType.DbFolder_Procedure:
                    {
                       
                            DataTable dt = BLLDbHelper.Instance.GetStroProcedure(ConnectStr, DataBaseType);
                            if(dt!=null)
                            foreach (DataRow item in dt.Rows)
                            {
                                base.Children.Add(new TableInfoViewModel(item["SpName"].ToString(), this));
                            }
                       
                        break;
                    }
                
            }

            //base.Children.Add(new ("表", this, NodeType.DbFolder_Table));           
        }

    }
    public class TableInfoViewModel : TreeViewItemViewModel
    {
        public TableInfoViewModel(string tbname, DbElementViewModel parent)
            : base(parent, true)
        {
            TableName = tbname;
            ConnectStr = parent.ConnectStr;
            DataBaseType = parent.DataBaseType;
        }
        public string TableName { get; set; }
        public string ConnectStr { get; set; }
        public NodeType DataBaseType { get; set; }
        public NodeType NodeType
        {
            get
            {
                return NodeType.DbTable;
            }
        }
        protected override void LoadChildren()
        {

            DataView dtv = BLLDbHelper.Instance.GetShemaColumn(ConnectStr, TableName, DataBaseType);         
            foreach (DataRowView item in dtv)
            {
                base.Children.Add(new DbColumnViewModel(item["Column_Name"].ToString(), this));
            }   
        }
    }
    public class ViewInfoViewModel : TreeViewItemViewModel
    {
        public ViewInfoViewModel(string name, DbElementViewModel parent)
            : base(parent, true)
        {
            ViewName = name;
            DataBaseType = parent.DataBaseType;
        }
        public string ViewName { get; set; }
        public NodeType DataBaseType { get; set; }
        public NodeType NodeType
        {
            get
            {
                return NodeType.DbView;
            }
        }
    }
    public class StroreProcedureViewModel : TreeViewItemViewModel
    {
        public StroreProcedureViewModel(string name, DbElementViewModel parent)
            : base(parent, true)
        {
            SpName = name;
            DataBaseType = parent.DataBaseType;
        }
        public string SpName { get; set; }
        public NodeType DataBaseType { get; set; }
    }

    public class DbColumnViewModel : TreeViewItemViewModel
    {
        public DbColumnViewModel(string name, TableInfoViewModel parent)
            : base(parent, true)
        {
            ColumnName = name;
            DataBaseType = parent.DataBaseType;
        }
        public NodeType DataBaseType { get; set; }
        public string ColumnName { get; set; }

        public NodeType NodeType 
        {
            get
            {
                return NodeType.DbColumn;
            }
        }
    }




}
